class Wander:
    def __init__(self, mongo_client, time_interval, wander_count, reset_time):
        self.wander_dic = {}
        self.mongo_client = mongo_client
        self.time_interval = time_interval
        self.wander_count = wander_count
        self.reset_time = reset_time

    def handel_logic(self, camera_id, timestamp, acvs_id, face_uri, picture_uri):
        if self.wander_dic.get(camera_id):
            current_cam = self.wander_dic[camera_id]
            if current_cam.get(acvs_id):
                if int(timestamp) - current_cam.get(acvs_id)[-1] > self.time_interval:
                    current_cam.get(acvs_id).append(int(timestamp))
                    # 剔除最大时间戳reset time之前的数据
                    current_cam.get(acvs_id).sort()
                    current_cam[acvs_id] = [item for item in current_cam.get(acvs_id) if
                                           current_cam.get(acvs_id)[-1] - item <= self.reset_time]
                    if len(current_cam.get(acvs_id)) >= self.wander_count:
                        wander_data = {
                            "acvs_id": acvs_id,
                            "camera_id": camera_id,
                            "begin_time": current_cam.get(acvs_id)[0],
                            "end_time": current_cam.get(acvs_id)[-1],
                            "face_image_uri": face_uri,
                            "picture_uri": picture_uri
                        }
                        self.mongo_client.ins_one_wander(wander_data)
                        del current_cam[acvs_id]

                        out_of_time_list = []
                        for camera, acvs in self.wander_dic.items():
                            for a in acvs:
                                if abs(self.wander_dic[camera][a][-1] - timestamp) > self.reset_time:
                                    out_of_time_list.append((camera, a))
                        for item in out_of_time_list:
                            del self.wander_dic[item[0]][item[1]]
        else:
            self.wander_dic[camera_id] = {acvs_id: [timestamp]}
